
/*-----------------------------------------------*
Name: Haqdarshak Pilot Intervention-Analysis (for Appendix)
Date Created: 30 July, 2018
Date Last Modified: 28 April, 2021
Created by: Aaron Berman and modified by Daniela Paz and Saumya Mathur on Nov.13/2020 
Uses Data: pilot_intervention_citizen_cumulative_all, pilot_intervention_citizen_cumulative_family, 
pilot_intervention_tracking_sheets_all and pilot_intervention_completed_apps
screening counts pilot_intervention_household_screening_counts.dta
Creates Data: Latex Tables 2 and 3 Appendix
Description: Creates warnings table and others
*-------------------------------------------------*/



*..................................................
**Install user-written commands
foreach package in mdesc nmissing veracrypt {
     capture which `package'
	 if _rc==111 ssc install `package'
}
*..................................................
clear all
set more off
version 12.0
cap log close
pause off



//open log 
*cd "$logs"
*log using "analysis_appendix_2_3.smcl", replace



***Appendix Tables 2 and 3

	use "$pilot/pilot_intervention_citizen_cumulative_all.dta", clear
	
	*************************
	destring household_id, replace //ADDED 13 SEPTEMBER
	//ACCOUNTING FOR FAMILY OBSERVATIONS THAT DO NOT HAVE ANY CITIZEN OBSERVATIONS 
	merge m:1 household_id using "$pilot/pilot_intervention_citizen_cumulative_family", gen(_merge_fam)
	pause


	*****************

	//Added 11/26: count if child <=18 years was screened in household 
	gen child = (citizen_age <= 18)
	gen child_screened = (discount_status == "yes") if child == 1 & _merge_fam != 2
	gen child_screened_warn = (warning_status == "yes") if child == 1 & _merge_fam != 2
	bysort household_id: egen num_child_screened = total(child_screened), missing 
	bysort household_id: egen num_child_screened_warn = total(child_screened_warn), missing 
	gen hh_child_screened_dummy = (num_child_screened > 0 & !missing(num_child_screened)) 
	gen hh_child_screened_dummy_warn = (num_child_screened_warn > 0  & !missing(num_child_screened_warn))
	pause

	//household-level data
	*SOLVED (BEFORE 1012201 and now 962722)
	bysort household_id (discount): assert discount[1] == discount[_N] | household_id == 962722 //ensure everyone in HH got same treatment (account for 1 anomaly we know of)
	gen takeup = (discount_status == "yes") if _merge_fam != 2
	gen warning_dummy = (warning == "yes") if _merge_fam != 2
	gen takeup_warning = (warning_status == "yes") if _merge_fam != 2
	bysort household_id: egen num_takeup = total(takeup), missing 
	bysort household_id: egen num_warning = total(warning_dummy), missing 
	bysort household_id: egen num_takeup_warning = total(takeup_warning), missing
	bysort household_id: gen num_offered = _N 

	//one obs per HH
	bysort household_id (screen_date): keep if _n == 1
	gen any_takeup = (num_takeup > 0 & _merge_fam != 2)
	gen any_warning = (num_warning > 0 & _merge_fam != 2)
	gen any_warning_takeup = (num_takeup_warning > 0 & _merge_fam != 2)

	//account for family-tab-only observations
	replace any_takeup = 1 if _merge_fam == 2 & discount_status_fam == "yes"
	/*
	replace any_warning = 1 if _merge_fam == 2 & warning_fam == "yes"
	replace any_warning_takeup = 1 if _merge_fam == 2 & warning_status_fam == "yes"
	*/
	destring discount_fam, replace
	replace discount = discount_fam if _merge_fam == 2

	//Added 9/25: Account for warning in regressions 
	gen takeup_dummy_use = any_takeup if any_warning == 0
	replace takeup_dummy_use = any_warning_takeup if any_warning == 1

	gen hh_child_screened_dummy_use = hh_child_screened_dummy if any_warning == 0
	replace hh_child_screened_dummy_use = hh_child_screened_dummy_warn if any_warning == 1
	
	gen num_child_screened_use = num_child_screened if any_warning == 0
	replace num_child_screened_use = num_child_screened_warn if any_warning == 1
	pause

	//matrix 
	matrix warning_screening = J(2, 4, .)
	forv i = 1/4 {
		local condition: word `i' of "1" "discount == 100" "discount == 50" "discount == 0"

		count if `condition' & any_takeup == 1 & any_warning == 0
		local num_group = `r(N)'
		*matrix warning_screening[1, `i'] = `r(N)'
		count if `condition' & any_takeup == 1 & any_warning == 0
		local num_takeup = `r(N)'
		local pct: di %8.1f (`num_takeup' / `num_group')*100
		matrix warning_screening[1, `i'] = `pct'

		count if `condition' & any_warning == 1
		local num_group = `r(N)'
		*matrix warning_screening[3, `i'] = `r(N)'
		count if `condition' & any_warning_takeup == 1 & any_warning == 1
		local num_takeup = `r(N)'
		local pct: di %8.1f (`num_takeup' / `num_group')*100
		matrix warning_screening[2, `i'] = `pct'
	}

	//output matrix to latex 
	cd "$Appe"

	#delimit ;
	esttab matrix(warning_screening) using "Table_A9.tex", 
		label booktabs
		collabels("Total" "Free" "Half Price" "Full Price") 
		varlabels(r1 "/% Take-up, No Warning" r2 "/% Take-up, Warning")
		mlabels(none) 
		replace;

	#delimit cr

	pause


	//tempfile and open tracking 
	tempfile hh_screenings 
	save `hh_screenings', replace 

	cd "$pilot"
	use "pilot_intervention_tracking_sheets_all", clear 

	merge m:1 member_id scheme_id using "$pilot/pilot_intervention_completed_apps"
	drop if _merge == 2 //there should be 2 observations here that don't merge 
	gen applied_scheme = (_merge == 2 | _merge == 3)
	drop _merge
	pause

	//collapse by household ID 
	collapse (sum) hh_num_agreed=agreed_scheme_amount hh_num_applied=applied_scheme, by(household_id)
	gen atleast1_scheme_agree = (hh_num_agreed > 0)
	gen atleast1_scheme_apply = (hh_num_applied > 0)
	replace household_id = 810125 if household_id == 401185

	merge 1:1 household_id using `hh_screenings'
	replace atleast1_scheme_agree = 0 if _merge == 2
	replace atleast1_scheme_apply = 0 if _merge == 2

	//matrix 
	matrix warning_scheme = J(2, 4, .)
	forv i = 1/4 {
		local condition: word `i' of "1" "discount == 100" "discount == 50" "discount == 0"

		count if `condition' & any_takeup == 1 & any_warning == 0
		local num_group = `r(N)'
		*matrix warning_screening[1, `i'] = `r(N)'
		count if `condition' & atleast1_scheme_apply == 1 & any_takeup == 1 & any_warning == 0
		local num_takeup = `r(N)'
		local pct: di %8.1f (`num_takeup' / `num_group')*100
		matrix warning_scheme[1, `i'] = `pct'

		count if `condition' & any_warning == 1
		local num_group = `r(N)'
		*matrix warning_screening[3, `i'] = `r(N)'
		count if `condition' & atleast1_scheme_apply == 1 & any_warning == 1
		local num_takeup = `r(N)'
		local pct: di %8.1f (`num_takeup' / `num_group')*100
		matrix warning_scheme[2, `i'] = `pct'
	}

	pause

	//output matrix to latex 
	cd "$Appe"

	#delimit ;
	esttab matrix(warning_scheme) using "Table_A10.tex", 
		label booktabs
		collabels("Total" "Free" "Half Price" "Full Price") 
		varlabels(r1 "/% Take-up, No Warning" r2 "/% Take-up, Warning")
		mlabels(none) 
		replace;

	#delimit cr

